package cn.itcast.nems.code.generator;

import java.io.File;
import java.util.Collections;

import org.springframework.util.StringUtils;

import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.TemplateConfig;

import cn.itcast.nems.common.entity.BaseEntity;


public class BaseGeneratorConfig {
    protected static DataSourceConfig dataSourceBuild(String dbUrl, int dbPort, String dbSchemaName, String dbUsername, String dbPassword) {
        String url = String.format("jdbc:mysql://%s:%s/%s?serverTimezone=Asia/Shanghai", dbUrl, dbPort, dbSchemaName);
        return new DataSourceConfig
                        .Builder(url, dbUsername, dbPassword)
                        .schema(dbSchemaName)
                        .build();
    }
    
    /**
     * 策略配置
     */
    protected static StrategyConfig strategyConfig(final String tableNames, final String tablePrefix) {
        return new StrategyConfig.Builder()
                                .addInclude(tableNames)
                                .addTablePrefix(tablePrefix)
                                
                                .entityBuilder()
                                .enableLombok()
                                .superClass(BaseEntity.class)
                                .addSuperEntityColumns("tenant_id", "subtenant_id", "created_by", "created_datetime", "modified_by", "modified_datetime", "deleted_by", "deleted_datetime", "deleted_flag")
                                .logicDeleteColumnName("deleted_flag")
                                
                                .serviceBuilder()
                                .formatServiceFileName("%sService")
                                .build();
    }
    
    /**
     * 全局配置
     */
    protected static GlobalConfig globalConfig(String author, String mavenSubModuleName) {
        String subModulePath = String.format("/%s/src/main/java", mavenSubModuleName);
        String projectRootDirectoryPath = getProjectRootDirectoryPath();
        String outputPath = StringUtils.hasLength(mavenSubModuleName) ? projectRootDirectoryPath.concat(subModulePath) : projectRootDirectoryPath;

        return new GlobalConfig.Builder()
                        .outputDir(outputPath)
                        .author(author)
                        .build();
    }

    /**
     * 包配置
     */
    protected static PackageConfig packageConfig(String packageName, String mavenSubModuleName) {
        String subModulePath = String.format("/%s/src/main/resources/mapper", mavenSubModuleName);
        String projectRootDirectoryPath = getProjectRootDirectoryPath();
        String mapperOutputPath = StringUtils.hasLength(mavenSubModuleName) ? projectRootDirectoryPath.concat(subModulePath) : projectRootDirectoryPath;
        
        return new PackageConfig.Builder()
                                .parent(packageName)
                                .pathInfo(Collections.singletonMap(OutputFile.xml, mapperOutputPath))
                                .entity("dao.entity")
                                .mapper("dao.mapper")
                                .build()
                                ;
    }

    /**
     * 模板配置
     */
    protected static TemplateConfig.Builder templateConfig() {
        return new TemplateConfig.Builder();
    }

    /**
     * 注入配置
     */
    protected static InjectionConfig.Builder injectionConfig() {
        // 测试自定义输出文件之前注入操作，该操作再执行生成代码前 debug 查看
        return new InjectionConfig.Builder().beforeOutputFile((tableInfo, objectMap) -> {
            System.out.println("tableInfo: " + tableInfo.getEntityName() + " objectMap: " + objectMap.size());
        });
    }
    
    protected static String getProjectRootDirectoryPath() {
        String currentDirectoryPath = System.getProperty("user.dir");
        return new File(currentDirectoryPath).getParent();
    }

    protected static String getProjectPath() {
        String currentDirectoryPath = System.getProperty("user.dir");
        //return new File(currentDirectoryPath).getParent();
        return new File(currentDirectoryPath).getPath();
    }
}
